package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DBUtil.DBUtils;
import dao.ISmsDao;
import entity.Course;
import entity.User;
import service.ISmsserivce;
import service.impl.SmsserivceImpl;

/**
 * Servlet implementation class Loginservlet
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/Loginservlet" })
public class Loginservlet extends HttpServlet {
	
	ISmsserivce smsserivce = new SmsserivceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String flag = request.getParameter("flag");
		User user = new User(username,password,flag);
		
		HttpSession session1 = request.getSession();
		
		
		int login = -1;
			try {
				 login = smsserivce.login(user);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		if(username !=null && password !=null) {
			session1.setAttribute("username", username);
			if(login > 0) {
				if("0".equals(flag)) {
					response.sendRedirect("index.jsp");
					
				}else if(flag.equals("1")) {
					response.sendRedirect("addshow.jsp");

					
				}
			}else {
				response.sendRedirect("login.jsp");
				System.out.println("登入失败");
			}
		}
		
		
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
